
/*
 *  
 *
 *  Copyright (C) Microsoft Corporation.  All rights reserved
 *
 *  
 */


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

namespace CRMScenarios
{
    /// <summary>
    /// Class for writing handlers  while the assembly-attached-document opens.
    /// </summary>
    public partial class ThisDocument
    {
		ActionsPane ap = null;

        static Office.CommandBar toolBar = null;
        static Office.CommandBarButton insFieldBtn = null;
        static Office.CommandBarButton attachDocBtn = null;

        /// <summary>
        /// Get the toolbars and buttons which are present on the application commanbar.
        /// If not present, report error.
        /// </summary>
        public static void GetToolBarAndButtons()
        {
            object missing = System.Reflection.Missing.Value;
            Word.Application application = Globals.ThisDocument.Application;

            try
            {
                toolBar = (Office.CommandBar)application.CommandBars[global::CRMScenarios.Resources.ToolBarName];
                insFieldBtn = (Office.CommandBarButton)toolBar.Controls[global::CRMScenarios.Resources.InsertButtonCaption]; // TODO
                attachDocBtn = (Office.CommandBarButton)toolBar.Controls[global::CRMScenarios.Resources.AttachButtonCaption];  // TODO
            }
            catch // (Exception Exp)
            { }

            if ((toolBar != null))
            {

                if (insFieldBtn != null)
                {
                    insFieldBtn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(insFieldBtn_Click);
                }

                if (attachDocBtn != null)
                {
                    attachDocBtn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(attachDocBtn_Click);
                }
            }
            else
            {
                Util.ShowMsg(global::CRMScenarios.Resources.ToolBarNotAvailable);
            }
        }

        /// <summary>
        /// Action to be taken on click of button
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="CancelDefault"></param>
        static void attachDocBtn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            if (Globals.ThisDocument.Application.ActiveDocument == Globals.ThisDocument.InnerObject)
            {
                BusinessEntity dummy = null;
                Globals.ThisDocument.ap.changeScenario(2, string.Empty, string.Empty, ref dummy, RefreshOption.FromTB); // 2 is for atttach doc.
            }
        }

        /// <summary>
        /// Action to be taken on click of button
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="CancelDefault"></param>
        static void insFieldBtn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            if (Globals.ThisDocument.Application.ActiveDocument == Globals.ThisDocument.InnerObject)
            {
                BusinessEntity dummy = null;
                Globals.ThisDocument.ap.changeScenario(1, string.Empty, string.Empty, ref dummy, RefreshOption.FromTB); // 1 is for insert field.
            }
        }

        /// <summary>
        /// Funtion called on start up of the docuemnt
        /// Do the initialization stuff here.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            ap = new ActionsPane();
            this.ActionsPane.Controls.Add(ap);
            ap.InitializeControls(this.Application, Globals.ThisDocument.InnerObject);
            GetToolBarAndButtons();
        }

        /// <summary>
        /// Clean up.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
            ap.ShutDownSetScenario();
            this.ActionsPane.Controls.Clear();
            ap.Dispose();
            try
            {
                insFieldBtn.Click -= new Office._CommandBarButtonEvents_ClickEventHandler(insFieldBtn_Click);
                attachDocBtn.Click -= new Office._CommandBarButtonEvents_ClickEventHandler(attachDocBtn_Click);
            }
            catch
            {
                //whatever the reason. we have to skip this. he already closed the app.
            }
        }
        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisDocument_Startup);
            this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
        }
        
        #endregion

    }
}
